//
//  DShopInfoViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-14.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "DShopInfoViewController.h"
#import "SelDeliveryDishViewController.h"
#import "DShopInfoCell.h"
#import "UIImageView+WebCache.h"
#import "MapViewController.h"

#define HeadView [self.view viewWithTag:5000]

@interface DShopInfoViewController ()

@end

@implementation DShopInfoViewController

- (id)initWithId:(NSString*)shopId
{
    self = [super initWithNibClass:[DShopInfoViewController class]];
    if (self) {
        self.shopId = shopId;
        _dataSource = [[DShopInfoDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
    [_dataSource fetchShopInfo];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:@"商户信息"];
    
    _infoTable.dataSource = _dataSource;
    _infoTable.delegate = _dataSource;
    
    [UIView hiddensYES:@[_theScrollview]];
    [UIView drawBorders:@[HeadView, _infoTable]];
    
    _theScrollview.height = ScreenHeight-NavBarHeight-StatusBarHeight;
}

- (IBAction)buttonPress:(id)sender
{
    SelDeliveryDishViewController *selDishView = [[SelDeliveryDishViewController alloc] initWithShopId:_shopId];
    selDishView.tel = self.tel;
    [self.navigationController pushViewController:selDishView animated:YES];
}

- (void)setFormValues:(NSDictionary*)data
{
    NSURL *url = [NSURL URLWithString:[data objectForKey:@"logo"]];
    
    [_logoImage setImageWithURL:url];
    _shopNameLabel.text = [data objectForKey:@"name"];
    _toPriceLabel.text = [NSString stringWithFormat:@"%@元起",[data objectForKey:@"biaozhun"]];
    _priceLabel.text = [data objectForKey:@"peisongfei"];
}

- (void)reLayoutSubviews
{
    [UIView hiddenNO:@[_theScrollview]];
    
    [_infoTable autoHeight];
    [_theScrollview autoHeight];
}
@end

//==========================================================================================
#pragma ===================数据源类实现=======================================================
//==========================================================================================

#define DShopInfoCellId @"DShopInfoCell"

@implementation DShopInfoDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if(self) {
        self.viewController = viewController;
        _shopInfo = [[NSDictionary alloc] init];
        _shopItemList = [[NSMutableArray alloc] initWithCapacity:5];
        self.shopInfoCell = [_viewController createCellWithNib:DShopInfoCellId];
    }
    return self;
}

- (void)fetchShopInfo
{
    NSString *url = [NSString stringWithFormat:API_DSHOP_INFO,_viewController.shopId];
    [self get:url];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@", request.responseString);
    NSDictionary *json = [request.responseData JSONValue];
    
    if ([[json objectForKey:@"error_code"] intValue] != 0) {
        return;
    }
    
    _shopInfo = json;
    
    [_viewController setFormValues:json];
    [self reBuilderData:json];
}

- (void)reBuilderData:(NSDictionary*)data
{
    NSString *tel = [data objectForKey:@"tel"];
    NSString *address = [data objectForKey:@"xianqu"];
    NSString *fanwei = [data objectForKey:@"fanwei"];
    NSString *shuoming = [data objectForKey:@"shuoming"];
    
    self.shopItemList = [NSMutableArray arrayWithObjects:
    [NSDictionary dictionaryWithObjectsAndKeys:@"商户电话",@"title",tel,@"text", nil],
    [NSDictionary dictionaryWithObjectsAndKeys:@"商户地址",@"title",address,@"text", nil],
    [NSDictionary dictionaryWithObjectsAndKeys:@"配送范围",@"title",fanwei,@"text", nil],
    [NSDictionary dictionaryWithObjectsAndKeys:@"配送说明",@"title",shuoming,@"text", nil],nil];
    
    _viewController.tel = tel;
    [_viewController.infoTable reloadData];
    [_viewController reLayoutSubviews];
}

#pragma mark-=====================================tableview===================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_shopInfoCell setContent:[_shopItemList objectAtIndex:indexPath.row] isLastRow:NO];
    return _shopInfoCell.height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _shopItemList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DShopInfoCell *cell = [_viewController createCellWithNib:DShopInfoCellId tableview:tableView];
    
    if (indexPath.row < 2) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    BOOL isLastRow = indexPath.row == _shopItemList.count-1;
    
    NSDictionary *dic = [_shopItemList objectAtIndex:indexPath.row];
    [cell setContent:dic isLastRow:isLastRow];
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [_shopItemList objectAtIndex:indexPath.row];
    if (indexPath.row == 0) {
        NSString *tel = [dic objectForKey:@"text"];
        tel = [NSString stringWithFormat:@"tel:%@", tel];
        
        UIWebView *callWebview =[[UIWebView alloc] init];
        NSURL *telURL =[NSURL URLWithString:tel];
        [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
        
        [_viewController.view addSubview:callWebview];
    }
    
    if (indexPath.row == 1) {
        MapViewController *mapView = [[MapViewController alloc] initWithShopInfo:_shopInfo];
        [_viewController pushViewController:mapView];
    }
}

@end
